// main.c
// Jedi Academy Server Log Converter - Version 1.1
// 
// Created By Steven Mattera on August 30th 2010.
// Copyright (c) 2010 Steven Mattera <stevenmattera@gmail.com>
//
// Jedi Academy Server Log Converter is free software; you can 
// redistribute it and/or modify it under the terms of the GNU General 
// Public License as published by the Free Software Foundation; either 
// version 2 of the License, or (at your option) any later version.
//
// Jedi Academy Server Log Converter is distributed in the hope that it 
// will be useful, but WITHOUT ANY WARRENTY; without even the implied 
// warrenty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more deails.
//
// You should have received a copy of the GNU General Public License
// along with Jedi Academy Server Log Converter; if not, write to the Free 
// Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
// USA
//

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <unistd.h>

FILE * openLogFile(char *path);
FILE * openConvFile(char *argv[]);

int main(int argc, char *argv[]) {
    FILE * logFile;
    FILE * convFile;

	printf("Jedi Academy Server Log Converter\n");
	printf("Copyright (c) 2010 Steven Mattera <stevenmattera@gmail.com>\n\n");
	printf("Jedi Academy Server Log Converter is free software; you can redistribute it\n");
	printf("and/or modify it under the terms of the GNU General Public License as published\n");
	printf("by the Free Software Foundation; either version 2 of the License, or (at your\n");
	printf("option) any later version.\n\n");
	printf("Jedi Academy Server Log Converter is distributed in the hope that it will be\n");
	printf("useful, but WITHOUT ANY WARRENTY; without even the implied warrenty of \n");
	printf("MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public\n");
	printf("License for more deails.\n\n");
	printf("You should have received a copy of the GNU General Public License along with\n");
	printf("Jedi Academy Server Log Converter; if not, write to the Free Software\n");
	printf("Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA\n\n");
    
	if(argc != 2) {
		printf("Usage: JALOG_CONV.EXE <PATH_TO_LOG>\n");
		system("pause");
		
		return -1;
	}

	logFile = openLogFile(argv[1]);
	if (logFile == NULL) return -1;
	
	convFile = openConvFile(argv);
	if (convFile == NULL) return -1;
		
	fputs("<html><head><style>body{background-color:gray}b{font-weight:normal}.r{color:red}.g{color:green}.y{color:yellow}.b{color:blue}.c{color:cyan}.m{color:magenta}.w{color:white}.l{color:black}</style></head><body><span class='w'>", convFile);

	char * line;
	char * lineProcessing;
	char * convLine;
	size_t currentPosition;
	int color = 7;
	
	while(!feof(logFile)) {
		line = (char *)malloc(sizeof(char)*512);
		memset(line, '\0', sizeof(char)*512);
		
		fgets(line, 512, logFile);
		if(color != 7) {
			fputs("</b><b class=w>", convFile);
			color = 7;
		}
		
		int n;
		for (n = 0; n < strlen(line); n++) {
			if(line[n] == '^') {
				switch (atoi(&line[n+1])) {
					case 1:
						if(color != 1) {
							color = 1;
							fputs("</b><b class='r'>", convFile);
						}
						break;
					case 2:
						if(color != 2) {
							color = 2;
							fputs("</b><b class='g'>", convFile);
						}
						break;
					case 3:
						if(color != 3) {
							color = 3;
							fputs("</b><b class=y'>", convFile);
						}
						break;
					case 4:
						if(color != 4) {
							color = 4;
							fputs("</b><b class='b'>", convFile);
						}
						break;
					case 5:
						if(color != 5) {
							color = 5;
							fputs("</b><b class='c'>", convFile);
						}
						break;
					case 6:
						if(color != 6) {
							color = 6;
							fputs("</b><b class='m'>", convFile);
						}
						break;
					case 7:
						if(color != 7) {
							color = 7;
							fputs("</b><b class='w'>", convFile);
						}
						break;
					case 0:
						if(color != 8) {
							color = 8;
							fputs("</b><b class='l'>", convFile);
						}
						break;
					default:
						fputc('^', convFile);
						fputc(line[n+1], convFile);
						break;
				}
				n++;
			}
			else if (line[n] == ':' && color != 7) {
				fputs("</b><b class='w'>", convFile);
				fputc(':', convFile);
			}
			else if (line[n] == '\\' && color != 7) {
				fputs("</b><b class='w'>", convFile);
				fputc('\\', convFile);
			}
			else if (line[n] == ' ' && line[n+1] == 'k' && line[n+2] == 'i' && line[n+3] == 'l' && line[n+4] == 'l' && line[n+5] == 'e' && line[n+6] == 'd' && line[n+7] == ' ' && color != 7) {
				fputs("</b><b class='w'>", convFile);
				fputc(' ', convFile);
			}
			else if (line[n] == ' ' && line[n+1] == 'b' && line[n+2] == 'y' && line[n+3] == ' ' && color != 7) {
				fputs("</b><b class='w'>", convFile);
				fputc(' ', convFile);
			}
			else if (line[n] == '<') {
				fputs("&lt;", convFile);
			}
			else if (line[n] == '>') {
				fputs("&gt;", convFile);
			}
			else if (line[n] == '&') {
				fputs("&amp;", convFile);
			}
			else if (line[n] != '\n' || line[n] != '\r') {
				fputc(line[n], convFile);
			}
		}
		
		fputs("<br>", convFile);
		
		free(line);
	}
	
	fputs("</body></html>", convFile);

	fclose(convFile);
	fclose(logFile);
	
	return 0;
}

FILE * openLogFile(char *path) {
	FILE * logFile = fopen(path, "r");
	
	if(logFile == NULL) {
		printf("Error: Opening reading stream.\n\n");
		system("pause");
		
		return NULL;
	} 

	return logFile;
}

FILE * openConvFile(char *argv[]) {
	char abs_exe_path[MAX_PATH];

	char * pch = strrchr(argv[0],'\\');
	strncpy(abs_exe_path, argv[0], pch-argv[0]+1);
	free(pch);
	
	char convPath[PATH_MAX];
	sprintf(convPath, "%sJALOG_CONV.HTML", abs_exe_path);
	FILE * convFile = fopen(convPath, "w+");
	
	
	if(convFile == NULL) {
		printf("Error: Opening writing stream.\n\n");
		system("pause");
		
		return NULL;
	}
	
	return convFile;
}
